<template>
  <div class="page-search">
    <hy-form v-bind="searchFormConfig" v-model="formData" ref="hyFrom">
      <template #header>
        <h1>高级检索</h1>
      </template>
        <template #footer>
        <div class="handle-btns">
          <el-button type="none" :icon="Refresh" @click="handleResetClick">重置</el-button>
          <el-button type="primary" :icon="Search" @click="handleQueryClick">搜索</el-button>
        </div>
      </template>
    </hy-form>
  </div>
</template>

<script lang="ts">
import { defineComponent , onMounted, ref} from "vue";
import HyForm from '@/base-ui/form'

import { Edit, Share, Delete, Search, Refresh } from '@element-plus/icons'

export default defineComponent({
  components:{
    HyForm
  },
  props:{
    searchFormConfig:{
      type:Object,
      required:true
    }
  },
  emits:['resetBtnClick','queryBtnClick'],
  setup(props,{ emit }) {
    //双向绑定的属性应该是由配置文件中的field来决定
    const formItem = props.searchFormConfig?.formItems ?? [];
    const formOriginData: any = {};
    for(const item of formItem){
      formOriginData[item.field] = ''
    }

    const formData = ref(formOriginData);
    const hyFrom = ref()
    //点击重置
    const handleResetClick = () => {
      //自己用了老师的俩种方式都没效果, tip:原来是我写错了，哈哈
      // for(const key in formOriginData){
      //   formData.value[`${key}`] = formOriginData[key]
      // }

      // formData.value = formOriginData;

      //自己用ref的方式重置
      hyFrom.value?.reset(formOriginData)

      emit('resetBtnClick')
    }
    //点击搜索
    const handleQueryClick = () => {
      console.log('点击搜索',formData.value)
      emit('queryBtnClick',formData.value)
    }
    return {
      formData,
      Search,
      Refresh,
      handleResetClick,
      hyFrom,
      handleQueryClick,
    }
  }
})
</script>

<style lang="less" scoped>
 .handle-btns{
    text-align: right;
    padding: 0 50px 20px 0;
  }
</style>
